package com.lanou.weekend02.service.impl;

import com.lanou.weekend02.bean.AjaxResult;
import com.lanou.weekend02.bean.User;
import com.lanou.weekend02.mapper.UserMapper;
import com.lanou.weekend02.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author lizhongren1.
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    private final Pattern NAME_PATTERN=Pattern.compile("^[a-zA-Z0-9_-]{4,16}$");

    @Override
    public boolean isUserExists(String uname) {

        int result = userMapper.findUserExist(uname);

        if (result > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean judgeUsername(String uname) {

        Matcher m=NAME_PATTERN.matcher(uname);

        if (uname == null || "".equals(uname)){
            return false;
        } else if (!m.matches()){
            return false;
        }
        return true;
    }

    private boolean judgeInfo(String reg, String str){
        Pattern p=Pattern.compile(reg);
        Matcher m=p.matcher(str);

        if (str == null || "".equals(str)){
            return false;
        } else if (!m.matches()){
            return false;
        }
        return true;
    }

    private boolean judgePwd(User user){

        String pwd = user.getPassword();
        String repwd = user.getRepwd();

        if (pwd == null || "".equals(pwd) || pwd.length()<3||pwd.length() >20){
            return false;
        }
        return true;
    }

    private boolean judgeRePwd(User user) {
        if (user.getPassword().equals(user.getRepwd())){
            return true;
        }
        return false;
    }

    @Override
    public boolean addNewUser(User user) {

        int i = userMapper.insertNewUser(user);
        if (i > 0){
            user.setPassword(null);
            user.setRepwd(null);
            return true;
        }
        return false;
    }

    @Override
    public boolean login(User user) {
        int result = userMapper.findUserForLogin(user);
        if (result > 0){

            User user1 = userMapper.selectUserByName(user.getUsername());

            user.setUid(user1.getUid());

            return true;
        }
        return false;
    }

    @Override
    public AjaxResult judgeUserInfo(User user) {

        String uname = user.getUsername();

        boolean bname = judgeUsername(uname);
        if (!bname){
            return new AjaxResult("用户名不符合要求",1002,uname);
        }

        boolean exists = isUserExists(uname);
        if (exists){
            return new AjaxResult("用户名已存在",1003,uname);
        }

        boolean b = judgePwd(user);
        if (!b){
            return new AjaxResult("密码不符合要求,请保证密码长度在6到20个字符之间",1004,null);
        }

        if (!judgeRePwd(user)){
            return new AjaxResult("两次密码不一致",1005,null);
        }

        boolean b1 = addNewUser(user);

        if (!b1){
            return new AjaxResult("注册失败,请检查网络",1006,null);
        }

        return new AjaxResult("注册成功",1000,user);
    }
}
